package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
/**
 * 
 * @author msjykf
 * 生成邮件待发任务
 * {"batch_id":"","requesttype": 1}
 * batch_id：批次号;requesttype:请求类型 1定时任务调度 2动态页面调度
 *
 */
public class EmailCreate implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(EmailCreate.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def jsonbody = Util.getRequestBody(request,'utf-8'); //获取报文
			if(jsonbody.equals("")) {
				return "{\"msg\": \"未获取到入参报文！\",\"code\": -2}"
			};
			JSONObject obj = JSONObject.fromObject(jsonbody); //解析报文
			def batch_id = Util.filterForObject(obj.get("batch_id")); //批次号
			if(batch_id.equals("")){
				return "{\"msg\": \"批次号不能为空！\",\"code\": -2}"
			};
			def map = eliteBean.chaxun_sql("select eb.template_id,nvl(eb.expectedstartdate,sysdate) expectedstartdate,dt.filename,dp.priority,dp.channel,dt.description,eb.createdate from email_send_batch eb left join doctemplate dt on eb.template_id=dt.doctemplate_id left join docproperty dp on dt.doctemplate_id=dp.doctemplate_id where eb.batch_id='"+batch_id+"'","dataSource");
			if(map.size()==0) {
				return "{\"msg\": \"无效的批次号！\",\"code\": -2}"
			};
			def template_id = Util.filterForObject(map[0].get("template_id")); //模板id
			if(template_id.equals("")){
				return "{\"msg\": \"邮件模板不能为空！\",\"code\": -2}"
			};
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			def expectedstartdate = df.format(map[0].get("expectedstartdate")) //期望发送时间
			def subject = map[0].get("description"); //邮件模板名称
			def createdate = map[0].get("createdate"); //批次创建时间
			def filename = map[0].get("filename"); //邮件模板存储文件名称
			def pipe = map[0].get("channel"); //发送通道
			def priority = map[0].get("priority"); //优先级
			//排除掉不需要邮件服务的客户
			eliteBean.update("delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty in (select customer_id from customer where email_shield_flag='1' and customer_id is not null)","dataSource");
			PropertiesUtil properties = new PropertiesUtil();
			def birthdaysmsid = properties.readValue("birthdaysmsid"); //生日祝福短信模板ID
			def birthdaymouldid = properties.readValue("birthdaymouldid"); //电子生日贺卡
			def duemuoldid = properties.readValue("duemuoldid"); //短期理财到期日通知
			def funddivmuoldid = properties.readValue("funddivmuoldid"); //基金分红通知
			def fundtramuoldid = properties.readValue("fundtramuoldid"); //基金交易确认
			def openaccmuoldid = properties.readValue("openaccmuoldid"); //开户通知书
			def fundstamuoldid = properties.readValue("fundstamuoldid"); //开放式基金对账单
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			def list = eliteBean.chaxun_sqlRowSet("select distinct sk_invpty,invpty_name,email,bk_fundaccount,agencyname,open_date from email_custinfo where batch_id='"+batch_id+"' and email is not null","dataSource");
			def smap = "";
			def sqlresult = "";
			def content = "";
			def totalnum = 0;
			def num = 0;
			def errornum = 0;
			def i = 0;
			def sk_invpty = ""; //客户编号
			def invpty_name = "";//客户名称
			def email = ""; //客户邮箱
			def fundaccount = ""; //基金账号
			def agencyname = "";//销售渠道
			def opendate = ""; //开户日期
			JSONObject jsonout = new JSONObject();
			JSONObject jsonin = new JSONObject();
			JSONArray arr = new JSONArray();
			jsonout.put("currentdate",Util.getNowString("yyyy-MM-dd"));//当前日期
			
			MapSqlParameterSource params = new MapSqlParameterSource(); //sql防注入方式
			params.addValue("batch_id",batch_id);
			params.addValue("template_id",template_id);
			params.addValue("priority",priority);
			params.addValue("pipe",pipe);
			params.addValue("expectedstartdate",expectedstartdate);
			params.addValue("subject",subject);
			if(template_id.equals(birthdaymouldid)) {
				//生日祝福短信批次处理
				smap = eliteBean.chaxun_sql("select batch_id from sms_send_batch ss left join triggermode tm on tm.doctemplate_id = ss.sms_id where sms_id='SMS007'and  to_char(sysdate,'hh24:mi')>=nvl(tm.sendtime,'00:00')  and createdate>trunc(sysdate)  and state=0","dataSource");
				if(smap.size()>0) {
					def smsbatchid = smap[0].get("batch_id");
					String[] data=new String[2];
					data[0] = "insert into sms_send_sync(rec_guid,batch_id,sms_id,sk_invpty,invpty_name,mobile_phone,sms_content,createdate) select sysguid,'"+smsbatchid+"','"+birthdaysmsid+"',ec.sk_invpty,ec.invpty_name,ec.mobile_phone,replace(st.content,'[c_custname]',ec.invpty_name),sysdate from email_custinfo ec left join sms_template st on st.sms_id='"+birthdaysmsid+"' where ec.batch_id='"+batch_id+"' and ec.mobile_phone is not null";
					data[1] = "update sms_send_batch set state=1,acceptdate=sysdate,comments='执行成功',messages=(select count(*) from sms_send_sync where batch_id='"+smsbatchid+"') where batch_id='"+smsbatchid+"'";
					int[] dataresult = eliteBean.updateBatch(data,"dataSource");
				};
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					try {
						jsonout.put("customername",invpty_name);
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[2];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"'";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"'";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
						num++;
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}else if (template_id.equals(duemuoldid)) {
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					fundaccount = Util.filterForObject(list.getString(4));
					agencyname = Util.filterForObject(list.getString(5));
					jsonout.put("customername",invpty_name);
					jsonout.put("fundaccount",fundaccount);
					jsonout.put("agentcyname",agencyname);
					try {
						smap = eliteBean.chaxun_sql("select product_cname,bk_product,agencyname,dueshares,due_date from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("bk_product")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("dueshares")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("due_date")));
							jsonin.put("c6","");
							arr.add(jsonin);
						}
						jsonout.put("table",arr);
						arr = new JSONArray();
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[4];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[2] = "insert into email_st_maturity_his(rec_guid,batch_id,sk_invpty,bk_product,product_cname,agencyname,dueshares,due_date,createdate) select rec_guid,batch_id,sk_invpty,bk_product,product_cname,agencyname,dueshares,due_date,createdate from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[3] = "delete from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
						num++;
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}else if (template_id.equals(funddivmuoldid)) {
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					fundaccount = Util.filterForObject(list.getString(4));
					agencyname = Util.filterForObject(list.getString(5));
					jsonout.put("customername",invpty_name);
					jsonout.put("fundaccount",fundaccount);
					jsonout.put("agentcyname",agencyname);
					try {
						smap = eliteBean.chaxun_sql("select reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("reg_date")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("bonus_type")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("div_date")));
							jsonin.put("c6",Util.filterForObject(smap[i].get("total_shares")));
							jsonin.put("c7",Util.filterForObject(smap[i].get("capital")));
							jsonin.put("c8",Util.filterForObject(smap[i].get("netvalue")));
							jsonin.put("c9",Util.filterForObject(smap[i].get("real_shares")));
							arr.add(jsonin);
						}
						jsonout.put("table",arr);
						arr = new JSONArray();
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[4];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[2] = "insert into email_funddiv_confirm_his(rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares,createdate) select rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares,createdate from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[3] = "delete from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
						num++;
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}else if (template_id.equals(fundtramuoldid)) {
				smap = eliteBean.chaxun_sql("select max(date_d) traday from c_calendar where workflag=1 and date_d<to_date('"+sdf.format(createdate)+"','yyyy-mm-dd')","dataSource");
				def traday = sdf.format(smap[0].get("traday")); //获取最后一个交易日
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					fundaccount = Util.filterForObject(list.getString(4));
					agencyname = Util.filterForObject(list.getString(5));
					jsonout.put("customername",invpty_name);
					jsonout.put("fundaccount",fundaccount);
					jsonout.put("agentcyname",agencyname);
					jsonout.put("confirm_date",traday);
					try {
						smap = eliteBean.chaxun_sql("select req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("req_date")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("tradetype")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("req_balance")));
							jsonin.put("c6",Util.filterForObject(smap[i].get("tradefare")));
							jsonin.put("c7",Util.filterForObject(smap[i].get("netvalue")));
							jsonin.put("c8",Util.filterForObject(smap[i].get("confirm_balance")));
							jsonin.put("c9",Util.filterForObject(smap[i].get("confirm_shares")));
							jsonin.put("c10",Util.filterForObject(smap[i].get("transtatus")));
							jsonin.put("c11",Util.filterForObject(smap[i].get("cause")));
							arr.add(jsonin);
						}
						jsonout.put("table",arr);
						arr = new JSONArray();
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[4];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[2] = "insert into email_fundtrade_confirm_his(rec_guid,batch_id,sk_invpty,req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause,createdate) select rec_guid,batch_id,sk_invpty,req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause,createdate from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[3] = "delete from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
						num++;
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}else if (template_id.equals(openaccmuoldid)) {
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					fundaccount = Util.filterForObject(list.getString(4));
					agencyname = Util.filterForObject(list.getString(5));
					opendate = Util.filterForObject(list.getInt(6));
					jsonout.put("customername",invpty_name);
					jsonout.put("fundaccount",fundaccount);
					jsonout.put("agentcyname",agencyname);
					jsonout.put("opendate",opendate);
					try {
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[2];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
						num++;
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}else if (template_id.equals(fundstamuoldid)) {
				Calendar cal1 = Calendar.getInstance();
				cal1.add(Calendar.MONTH, -1);
				cal1.set(Calendar.DAY_OF_MONTH,1);
				def firstDay = sdf.format(cal1.getTime()); //取上月第一天
				Calendar cal2 = Calendar.getInstance();
				cal2.set(Calendar.DAY_OF_MONTH, 0);
				def lastDay = sdf.format(cal2.getTime()); //取上月最后一天
				list = eliteBean.chaxun_sqlRowSet("select distinct sk_invpty,invpty_name,email,bk_fundaccount,agencyname from email_custinfo where batch_id='"+batch_id+"' and email is not null","dataSource");
				while(list.next()){
					totalnum++;
					sk_invpty = Util.filterForObject(list.getInt(1));
					invpty_name = Util.filterForObject(list.getString(2));
					email = Util.filterForObject(list.getString(3));
					fundaccount = Util.filterForObject(list.getString(4));
					agencyname = Util.filterForObject(list.getString(5));
					jsonout.put("customername",invpty_name);
					jsonout.put("fundaccount",fundaccount);
					jsonout.put("agentcyname",agencyname);
					jsonout.put("startdate",firstDay);
					jsonout.put("enddate",lastDay);
					num++;
					try {
						smap = eliteBean.chaxun_sql("select product_cname,agencyname,bonus_type,shares,netvalue,capital,income from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("bonus_type")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("shares")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("netvalue")));
							jsonin.put("c6",Util.filterForObject(smap[i].get("capital")));
							jsonin.put("c7",Util.filterForObject(smap[i].get("income")));
							arr.add(jsonin);
						}
						jsonout.put("tablea",arr);
						jsonin = new JSONObject();
						arr = new JSONArray();
						smap = eliteBean.chaxun_sql("select req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("req_date")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("confirm_date")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("bk_product")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c6",Util.filterForObject(smap[i].get("tradetype")));
							jsonin.put("c7",Util.filterForObject(smap[i].get("amount")));
							jsonin.put("c8",Util.filterForObject(smap[i].get("shares")));
							jsonin.put("c9",Util.filterForObject(smap[i].get("netvalue")));
							jsonin.put("c10",Util.filterForObject(smap[i].get("tradefare")));
							arr.add(jsonin);
						}
						jsonout.put("tableb",arr);
						jsonin = new JSONObject();
						arr = new JSONArray();
						smap = eliteBean.chaxun_sql("select reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,real_shares from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
						for(i=0;i<smap.size();i++) {
							jsonin.put("c1",Util.filterForObject(smap[i].get("reg_date")));
							jsonin.put("c2",Util.filterForObject(smap[i].get("product_cname")));
							jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
							jsonin.put("c4",Util.filterForObject(smap[i].get("bonus_type")));
							jsonin.put("c5",Util.filterForObject(smap[i].get("div_date")));
							jsonin.put("c6",Util.filterForObject(smap[i].get("total_shares")));
							jsonin.put("c7",Util.filterForObject(smap[i].get("total_profit")));
							jsonin.put("c8",Util.filterForObject(smap[i].get("netvalue")));
							jsonin.put("c9",Util.filterForObject(smap[i].get("real_shares")));
							arr.add(jsonin);
						}
						jsonout.put("tablec",arr);
						content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","gbk");
						params.addValue("sk_invpty",sk_invpty);
						params.addValue("invpty_name",invpty_name);
						params.addValue("email",email);
						params.addValue("content",content);
						eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
						String[] data=new String[8];
						data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[2] = "insert into email_fundbalanceinfo_his(rec_guid,batch_id,sk_invpty,product_cname,agencyname,req_balance,bonus_type,shares,netvalue,capital,income,createdate) select rec_guid,batch_id,sk_invpty,product_cname,agencyname,req_balance,bonus_type,shares,netvalue,capital,income,createdate from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[3] = "delete from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[4] = "insert into email_fundtradeinfo_his(rec_guid,batch_id,sk_invpty,req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare,createdate) select rec_guid,batch_id,sk_invpty,req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare,createdate from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[5] = "delete from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[6] = "insert into email_funddivinfo_his(rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,real_shares,createdate) select rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,real_shares,createdate from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						data[7] = "delete from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
						int[] dataresult = eliteBean.updateBatch(data,"dataSource");
					}catch(Exception e) {
						errornum++;
						log.error("ErrorMessage:",e);
					}
				};
			}
			eliteBean.update("update email_send_batch set messages="+totalnum+",quantityvalid=quantityvalid+"+num+",writefailed="+errornum+",state=3 where batch_id='"+batch_id+"'","dataSource");
			return "{\"msg\": \"请求接口成功！\",\"code\": 1}"
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}"
		}
	}
}	